package com.shelpe.services.sinbad.service.utils;

import java.util.Date;

import javax.persistence.AttributeConverter;

import com.shelpe.services.sinbad.entity.jpa.Adgroup.Status;
import com.shelpe.services.sinbad.entity.jpa.DriveType;
import com.shelpe.services.sinbad.entity.jpa.OnlineStatus;

public abstract class EntityUtils {

	
	public static class Day2IntConverter  implements AttributeConverter<Date, Integer>{

		@Override
		public Integer convertToDatabaseColumn(Date attribute) {
			return (int) (attribute.getTime() / 1000);
		}

		@Override
		public Date convertToEntityAttribute(Integer dbData) {
			long ts = dbData.intValue();
			return new Date(ts * 1000);
		}
		
	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute != null ? attribute.getValue() : Status.blank.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			return Status.from(dbData);
		}
	}
	
	public static class OnlineStatusConverter implements AttributeConverter<OnlineStatus, String> {

		@Override
		public String convertToDatabaseColumn(OnlineStatus attribute) {
			return attribute != null ? attribute.getValue() : OnlineStatus.online.getValue();
		}

		@Override
		public OnlineStatus convertToEntityAttribute(String dbData) {
			return OnlineStatus.from(dbData);
		}
	}
	
	public static class DriveTypeConverter implements AttributeConverter<DriveType, Integer> {

		@Override
		public Integer convertToDatabaseColumn(DriveType attribute) {
			return attribute != null ? attribute.getValue() : DriveType.manual.getValue();
		}

		@Override
		public DriveType convertToEntityAttribute(Integer dbData) {
			return DriveType.from(dbData);
		}
	}
	
	
	public static class Boolean2StringConverter implements AttributeConverter<Boolean, String> {

		@Override
		public String convertToDatabaseColumn(Boolean attribute) {
			return attribute.booleanValue() ? "true" : "false";
		}

		@Override
		public Boolean convertToEntityAttribute(String dbData) {
			return ("false".equals(dbData))?Boolean.FALSE:Boolean.TRUE;
		}
	}
	
}
